package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.ParseException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxuwz.medical.domain.family.Family;
import com.gxuwz.medical.domain.person.Person;
import com.gxuwz.medical.domain.user.User;
import com.gxuwz.medical.vo.PageBean;

public class PersonServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String method = request.getParameter("method");
		try {
			if(method.equals("list")){
				list(request,response);
			}else if(method.equals("add")){
				add(request,response);
			}else if(method.equals("del")){
				del(request,response);
			}else if(method.equals("edit")){
				edit(request,response);
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void jump(HttpServletRequest request, HttpServletResponse response,String path) throws ServletException, IOException, SQLException{
		Person p = new Person();
		String page = request.getParameter("page");
		if (page == null)
			page = "1";
		int currentpage = Integer.parseInt(page);
		PageBean pageBean = p.queryAll(currentpage, 5);
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher(path).forward(request, response);
	}
	public void jumpFamily(HttpServletRequest request, HttpServletResponse response,String path) throws ServletException, IOException, SQLException{
		Family f = new Family();
		String page = request.getParameter("page");
		if (page == null)
			page = "1";
		int currentpage = Integer.parseInt(page);
		PageBean pageBean = f.queryAll(currentpage, 5);
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher(path).forward(request, response);
	}

	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException{
		// TODO Auto-generated method stub
		String path = "/page/person/person_list.jsp";
		Person p = new Person();
		String page = request.getParameter("page");
		if (page == null)
			page = "1";
		int currentpage = Integer.parseInt(page);
		PageBean pageBean = p.queryAll(currentpage, 5);
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher(path).forward(request, response);
	}


	private void add(HttpServletRequest request, HttpServletResponse response) throws SQLException, ParseException, ServletException, IOException {
		// TODO Auto-generated method stub
		String path = "/page/family/family_list.jsp";
		String msg = "添加失败";
		String familycode = request.getParameter("familycode");
		String fullname = request.getParameter("fullname");
		String idcard = request.getParameter("idcard");
		String relative = request.getParameter("relative");
		String sex=request.getParameter("sex");
		String nation=request.getParameter("nation");
		String health=request.getParameter("health");
		String culture=request.getParameter("culture");
		String attribute=request.getParameter("attribute");
		String farmer=request.getParameter("farmer");
		String marry=request.getParameter("marry");
		String workunit=request.getParameter("workunit");
		String tel=request.getParameter("tel");
		Family f = new Family();
		f.setFamilycode(familycode);
		Person p = new Person(fullname, idcard,relative, sex, nation, health, culture, attribute, farmer, marry, workunit, tel);
		p.setFamily(f);
		p.add();
		jumpFamily(request, response, path);
	}


	private void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException{
		// TODO Auto-generated method stub
		String path = "/page/person/person_list.jsp";
		String idcard = request.getParameter("idcard");
		String familycode = request.getParameter("familycode");
		idcard = new String(idcard.getBytes("ISO-8859-1"),"UTF-8");
		familycode = new String(familycode.getBytes("ISO-8859-1"),"UTF-8");
		Person p = new Person();
		Family f = new Family();
		f.setFamilycode(familycode);
		p.setFamily(f);
		p.del(idcard);
		jump(request,response,path);
	}
	
	private void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException, ParseException{
		// TODO Auto-generated method stub
		String path = "/page/person/person_list.jsp";
		String msg = "修改失败";
		String ybcardno = request.getParameter("ybcardno");
		String familycode=request.getParameter("familycode");
		String fullname=request.getParameter("fullname");
		String idcard=request.getParameter("idcard");
		String relative=request.getParameter("relative");
		String sex=request.getParameter("sex");
		String nation=request.getParameter("nation");
		String health=request.getParameter("health");
		String culture=request.getParameter("culture");
		String attribute=request.getParameter("attribute");
		String farmer=request.getParameter("farmer");
		String marry=request.getParameter("marry");
		String workunit=request.getParameter("workunit");
		String tel=request.getParameter("tel");
		Family f = new Family();
		f.setFamilycode(familycode);
		Person p= new Person(fullname, idcard, relative, sex, nation, health, culture, attribute, farmer, marry, workunit, tel);
		p.setYbcardno(ybcardno);
		p.setFamily(f);
		p.update();
		jump(request,response,path);
	}

}
